package co.dreamapps.dodoattack.model;

import co.dreamapps.dodoattack.activities.GameActivity;
import co.dreamapps.dodoattack.model.Dodo;
import protocol.Print;
import protocol.RoomConnection;
import protocol.XmlParser;

public class RoomManagerDodoGame {

	/**
	 * Message constants
	 */
	private static final String TAG = "TAG";
	private static final String VALUE = "VAL";
	private static final String JUMP = "JMP";
	private static final String HORIZONTAL_MOVE = "HZN";
	private static final String EGG = "EGG";
	private static final String EGG_CRASH = "ESH";
	private static final String CORN_CHOISE = "CRN";
	private static final String GAME_STATE = "GST";
	private static final String BEGINNING = "BGN";
	private static final String DISCONNECT_ALERT = "DCN";

	/**
	 * Entities
	 */
	public Dodo dodo;
	private GameActivity game;

	/**
	 * Variables
	 */
	private long lastHorizontalUpdate;

	/**
	 * Connection
	 */
	private RoomConnection room;
	private boolean isRacing;

	public RoomManagerDodoGame(GameActivity game, RoomConnection room, boolean isRacing) {
		this.game = game;
		this.room = room;
		this.isRacing = isRacing;
	}

	public void dodoJump(int jumpingTimes, int actualY, int time, boolean priority) {
		room.sendMessage('<' + TAG + '>' + JUMP + "</" + TAG + "><" + VALUE + '>' + jumpingTimes + " " + actualY + " " + time + "</" + VALUE + ">", priority ? 2 : 1);
	}

	public boolean dodoHorizontalMove(int aimedX) {
		if (System.currentTimeMillis() - lastHorizontalUpdate > 250) {
			room.sendMessage('<' + TAG + '>' + HORIZONTAL_MOVE + "</" + TAG + "><" + VALUE + '>' + aimedX + "</" + VALUE + ">");
			lastHorizontalUpdate = System.currentTimeMillis();
			return true;
		}
		return false;
	}

	public void throwEgg(int eggId, boolean leftTrueFallingFalse, int x, int y, int totalTime) {
		room.sendMessage('<' + TAG + '>' + EGG + "</" + TAG + "><" + VALUE + '>' + eggId + " " + x + " " + y + " " + totalTime + " " + (leftTrueFallingFalse ? 1 : 0) + "</" + VALUE + ">");
	}

	public void crashEgg(int eggId, int x, int y) {
		room.sendMessage('<' + TAG + '>' + EGG_CRASH + "</" + TAG + "><" + VALUE + '>' + eggId + " " + x + " " + y + "</" + VALUE + ">", 2);
	}

	public void cornChoise(int counter, int chosenPath) {
		room.sendMessage('<' + TAG + '>' + CORN_CHOISE + "</" + TAG + "><" + VALUE + '>' + counter + " " + chosenPath + "</" + VALUE + ">");
	}
	
	public void sendBeginningTime(int actualTime) {
		room.sendMessage('<' + TAG + '>' + BEGINNING + "</" + TAG + "><" + VALUE + '>' + actualTime + "</" + VALUE + ">", 2);
	}
	
	public void sendGameState(int control, int life, int corn) {
		room.sendMessage('<' + TAG + '>' + GAME_STATE + "</" + TAG + "><" + VALUE + '>' + control + " " + life + " " + corn + "</" + VALUE + ">", 2);
	}
	
	public void sendDisconnectAlert() {
		room.sendMessage('<' + TAG + '>' + DISCONNECT_ALERT + "</" + TAG + "><" + VALUE + '>' + "</" + VALUE + ">", 2);
	}

	public void onNewMessage(String message) {
		try {
			String tag = XmlParser.readByTag(message, TAG, 0);
			String value = XmlParser.readByTag(message, VALUE, XmlParser.guessNextLimit(TAG, tag, 0));
			if (tag == null || value == null) {
				Print.print("Mensaje no reconocido: "+message);
				return;
			} else {
				Print.print("Tag: " + tag + " Value: " + value);
			}
			if (isRacing) {
				if (tag.equals(EGG)) {
					String parts[] = value.split(" ");
					int eggId = Integer.valueOf(parts[0]);
					int x = Integer.valueOf(parts[1]);
					int y = Integer.valueOf(parts[2]);
					int time = Integer.valueOf(parts[3]);
					boolean leftTrueFallingFalse = Integer.valueOf(parts[4]) == 1;
					game.addEgg(eggId, leftTrueFallingFalse, x, y, time);
				} else if(tag.equals(GAME_STATE)) {
					game.gameControlMessage(Integer.valueOf(value), 0, 0);
				} else if(tag.equals(DISCONNECT_ALERT)) {
					game.disconnectionMessage();
				}
			} else {
				if (tag.equals(JUMP)) {
					String parts[] = value.split(" ");
					int jumpingTimes = Integer.valueOf(parts[0]);
					int actualY = Integer.valueOf(parts[1]);
					int time = Integer.valueOf(parts[2]);
					dodo.jump(jumpingTimes, actualY, time);
				} else if (tag.equals(HORIZONTAL_MOVE)) {
					int aimedX = Integer.valueOf(value);
					dodo.setRemoteHorizontalMove(aimedX);
				} else if(tag.equals(EGG_CRASH)) {
					String parts[] = value.split(" ");
					int eggId = Integer.valueOf(parts[0]);
					int x = Integer.valueOf(parts[1]);
					int y = Integer.valueOf(parts[2]);
					game.crashEgg(eggId, x, y);
				} else if(tag.equals(CORN_CHOISE)) {
					String parts[] = value.split(" ");
					int counter = Integer.valueOf(parts[0]);
					int chosenPath = Integer.valueOf(parts[1]);
					game.chooseCorn(counter, chosenPath);
				} else if(tag.equals(GAME_STATE)) {
					String parts[] = value.split(" ");
					int control = Integer.valueOf(parts[0]);
					int life = Integer.valueOf(parts[1]);
					int corn = Integer.valueOf(parts[2]);
					room.sendMessage('<' + TAG + '>' + GAME_STATE + "</" + TAG + "><" + VALUE + '>' + control + "</" + VALUE + ">", 2);
					game.gameControlMessage(control, life, corn);
				} else if(tag.equals(BEGINNING)) {
					game.beginningGame(Integer.valueOf(value));
				} else if(tag.equals(DISCONNECT_ALERT)) {
					game.disconnectionMessage();
				}
			}
		} catch (Exception e) {
			Print.print("RoomManagerDododGame - onNewMessage() - Exception!!");
		}
	}

}
